import { type VulnerabilityViewItem } from "~/components/VulnerabilityTableViews";
import { type VulnerabilityTableData } from "~/types/scanTypes";
import { type VulnTableData } from "~/pages/vulnerabilities";

/**
 * Converts vulnerabilities from the Scanner page format to the common format
 */
export function adaptScannerVulnerabilities(
  vulns: any[]
): VulnerabilityViewItem[] {
  return vulns.map((vuln) => ({
    id: vuln.cve || vuln.vid || vuln.title || "unknown",
    title: vuln.cve || vuln.title || "Unknown Vulnerability",
    description: vuln.description || "No description available",
    severity: standardizeSeverity(vuln.severity || "MEDIUM"),
    riskScore:
      typeof vuln.riskScore !== "undefined"
        ? vuln.riskScore
        : typeof vuln.cvss !== "undefined"
        ? vuln.cvss
        : 0,
    affectedHosts: vuln.affectedHosts || vuln.hostCount || vuln.count || 0,
    published: vuln.published || "",
  }));
}

/**
 * Converts vulnerabilities from the Vulnerability page format to the common format
 */
export function adaptVulnerabilityPageData(
  vulns: any[]
): VulnerabilityViewItem[] {
  return vulns.map((vuln) => ({
    id: vuln.cve || vuln.vid || vuln.title || "unknown",
    title: vuln.cve || vuln.title || "Unknown Vulnerability",
    description: vuln.description || "No description available",
    severity: standardizeSeverity(vuln.severity || "MEDIUM"),
    riskScore:
      typeof vuln.riskScore !== "undefined"
        ? vuln.riskScore
        : typeof vuln.cvss !== "undefined"
        ? vuln.cvss
        : 0,
    affectedHosts: vuln.affectedHosts || vuln.hostCount || vuln.count || 0,
    published: vuln.published || "",
    lastModified: vuln.lastModified || "",
    references: vuln.references || [],
  }));
}

/**
 * Simple severity sorter function that can be used with array.sort()
 */
export function sortBySeverity(
  a: VulnerabilityViewItem,
  b: VulnerabilityViewItem
): number {
  const severityOrder = {
    CRITICAL: 0,
    HIGH: 1,
    MEDIUM: 2,
    LOW: 3,
    INFORMATIONAL: 4,
  };

  const aSeverity = a.severity.toUpperCase();
  const bSeverity = b.severity.toUpperCase();

  return (
    (severityOrder[aSeverity as keyof typeof severityOrder] || 999) -
    (severityOrder[bSeverity as keyof typeof severityOrder] || 999)
  );
}

/**
 * Helper to convert any format of severity to standardized format
 */
export function standardizeSeverity(severity: string): string {
  if (!severity) return "MEDIUM";

  const normalized = severity.toUpperCase();

  if (
    normalized === "CRITICAL" ||
    normalized === "HIGH" ||
    normalized === "MEDIUM" ||
    normalized === "LOW" ||
    normalized === "INFORMATIONAL"
  ) {
    return normalized;
  }

  // Handle common variations
  if (normalized.includes("CRIT")) return "CRITICAL";
  if (normalized.includes("HIGH")) return "HIGH";
  if (normalized.includes("MED")) return "MEDIUM";
  if (normalized.includes("LOW")) return "LOW";
  if (normalized.includes("INFO")) return "INFORMATIONAL";

  // Default
  return "MEDIUM";
}
